//
//  XCLeftSetingController.m
//  TTLestening
//
//  Created by dllo on 16/3/21.
//  Copyright © 2016年 dilo. All rights reserved.
//

#import "XCLeftSetingController.h" 
#import "XCSignInController.h"
#import <RESideMenu.h>
#import <AVOSCloud.h>
#import "XCUserController.h"
@interface XCLeftSetingController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic, strong) UIView *viewForLogIn;
@property (nonatomic, strong) UIButton *buttonForLogIn;
@property (nonatomic, strong) UILabel *LabelLogIn;
@property (nonatomic, copy) NSArray *arrForLabel;
@property (nonatomic, copy) NSArray *arrForImage;
@property (nonatomic, strong) UIButton *signIn;
@property (nonatomic, strong) UIButton *back;
@end

@implementation XCLeftSetingController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   // self.view.backgroundColor = [UIColor redColor];
    
    [self handleData];
    [self createTableView];
    
    [[NSNotificationCenter defaultCenter] addObserverForName:@"degnlu" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
       
        AVUser *currentUser = [AVUser currentUser];
        if (currentUser != nil) {
            // 跳转到首页
            self.signIn.alpha = 0;
            self.LabelLogIn.alpha = 1;
            self.back.alpha = 1;
            self.LabelLogIn.text = currentUser.username;
            
        } else {
            //缓存用户对象为空时，可打开用户注册界面…
            self.signIn.alpha = 1;
            self.LabelLogIn.alpha = 0;
            self.back.alpha = 0;
            
        }
        
    }];
    
    
}

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    
    [self havedLoginIn];
    
}

- (void)havedLoginIn {
    
   
    AVUser *currentUser = [AVUser currentUser];
    if (currentUser != nil) {
        // 跳转到首页
          self.signIn.alpha = 0;
        self.LabelLogIn.alpha = 1;
        self.back.alpha = 1;
        self.LabelLogIn.text = currentUser.username;
        
    } else {
        //缓存用户对象为空时，可打开用户注册界面…
        self.signIn.alpha = 1;
        self.LabelLogIn.alpha = 0;
        self.back.alpha = 0;
        
    }
    
}

- (void)handleData {
    
    self.arrForImage = @[@"iconfont-bofang-4", @"iconfont-bofang-4", @"iconfont-bofang-4", @"iconfont-bofang-4", @"iconfont-bofang-4", @"iconfont-bofang-4", @"iconfont-bofang-4", @"iconfont-bofang-4"];
    
    self.arrForLabel = @[@"导入歌曲", @"听歌识曲", @"更换壁纸", @"睡眠设置", @"音效", @"付费包月", @"流量包", @"设置"];
    
}
- (void)createTableView {
    
    self.viewForLogIn = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
    
    self.viewForLogIn.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.9];
    [self.view addSubview:self.viewForLogIn];
    
    self.buttonForLogIn = [UIButton buttonWithType:UIButtonTypeSystem];
    self.buttonForLogIn.frame = CGRectMake(20, 30, 80, 80);
    
   // self.buttonForLogIn.backgroundColor = [UIColor blackColor];
    [self.buttonForLogIn setBackgroundImage:[UIImage imageNamed:@"aaa.jpg"] forState:UIControlStateNormal];
    
    self.LabelLogIn = [[UILabel alloc] initWithFrame:CGRectMake(110, 50, 120, 50)];
    self.LabelLogIn.text = @"登录成功";
    [self.viewForLogIn addSubview:self.LabelLogIn];
    self.LabelLogIn.textColor = [UIColor whiteColor];
    self.LabelLogIn.font=[UIFont systemFontOfSize:23];
    self.LabelLogIn.alpha = 0;
    
    self.signIn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.signIn.frame = CGRectMake(110, 50, 120, 50);
    [self.signIn setTitle:@"登录/注册" forState:UIControlStateNormal];
    [self.signIn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.viewForLogIn addSubview:self.signIn];
    // self.LabelLogIn.textColor = [UIColor whiteColor];
    self.signIn.titleLabel.font = [UIFont systemFontOfSize:23];
    [self.signIn addTarget:self action:@selector(toSignIn:) forControlEvents:UIControlEventTouchUpInside];
    
    self.back = [UIButton buttonWithType:UIButtonTypeCustom];
    self.back.frame = CGRectMake(110, 110, 50, 50);
    [self.viewForLogIn addSubview:self.back];
    [self.back setTitle:@"退出登录" forState:UIControlStateNormal];
    [self.back addTarget:self action:@selector(lastAction:) forControlEvents:UIControlEventTouchUpInside];
    self.back.alpha = 0;
    
    [self.buttonForLogIn addTarget:self action:@selector(LogIn:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.viewForLogIn addSubview:self.buttonForLogIn];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 150, self.view.frame.size.width, self.view.frame.size.height - 150) style:UITableViewStylePlain];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.9];
    
    [self.view addSubview:self.tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"signIn"];
    
}

- (void)lastAction:(UIButton *) button {
    
    NSLog(@"退出登录");
    
    [AVUser logOut];  //清除缓存用户对象
  //  AVUser *currentUser = [AVUser currentUser]; // 现在的currentUser是nil了
    
    self.signIn.alpha = 1;
    self.LabelLogIn.alpha = 0;
    self.back.alpha = 0;

    
}

- (void)LogIn:(UIButton *)button {
    
    NSLog(@"用户信息");
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushUserInfo" object:nil];
  [self.sideMenuViewController hideMenuViewController];
}

- (void)toSignIn:(UIButton *)button {
    NSLog(@"登录");

    [[NSNotificationCenter defaultCenter] postNotificationName:@"pushSign" object:nil];
    
    [self.sideMenuViewController hideMenuViewController];


}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.arrForLabel.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return tableView.frame.size.height/self.arrForLabel.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"signIn"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"signIn"];
    }
    
    cell.textLabel.text = self.arrForLabel[indexPath.row];
    
    cell.imageView.image = [UIImage imageNamed:self.arrForImage [indexPath.row]];
    
    cell.textLabel.textColor = [UIColor whiteColor];
    
    cell.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.6];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    
    }


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
